package com.ylyq.ylxx.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ylyq.ylxx.entity.YlyqAreaOceangine;
import com.ylyq.ylxx.entity.YlyqAudio;
import com.ylyq.ylxx.model.ResponseData;
import com.ylyq.ylxx.model.request.GetYlyqAreaRequest;
import com.ylyq.ylxx.service.IFileService;
import com.ylyq.ylxx.service.IYlyqAreaOceangineService;
import com.ylyq.ylxx.service.IYlyqAudioService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.net.UnknownHostException;

/**
 * <p>
 * 背景音乐库对应的路径 前端控制器
 * </p>
 *
 * @author xiaweichao
 * @since 2020-09-28
 */
@RestController
@RequestMapping("/ylyqAudio")
@Api(description = "背景音乐库对应的路径接口")
public class YlyqAudioController {
    @Value("${public.config.fileUploadPath}")
    private String fileUploadPath;
    @Value("${public.config.fileDownloadPath}")
    private String fileDownloadPath;
    @Autowired
    private IYlyqAudioService iYlyqAudioService;
    @Autowired
    private IFileService fileService;

    @PostMapping("/uploadAudio")
    @ApiOperation(value = "上传音乐背景素材", response = YlyqAreaOceangine.class)
    public ResponseData uploadAudio(@RequestParam("audioFlag") @ApiParam(value = "背景音乐对应的品类flag", required = true) Integer audioFlag, @RequestParam("file") MultipartFile file) {
        try {
            String fileUrl = fileService.uploadPhotoOrFile(file);
            if (StringUtils.isBlank(fileUrl))
                return new ResponseData(ResponseData.Result.FAIL);
            else {
                YlyqAudio ylyqAudio=new YlyqAudio();
                ylyqAudio.setAudioFlag(audioFlag);
                ylyqAudio.setAudioUrl(fileUrl.replace(fileDownloadPath,fileUploadPath));
                iYlyqAudioService.save(ylyqAudio);
                return new ResponseData(ResponseData.Result.SUCCESS, fileUrl);
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
            return new ResponseData(ResponseData.Result.FAIL);
        }
    }

}
